//
//  HTTPMethod.m
//  NSURLSession版网络请求(AFN3.0)
//
//  Created by dllo on 15/10/27.
//  Copyright © 2015年 CY. All rights reserved.
//

#import "HTTPMethod.h"

@implementation HTTPMethod

+(void)getDataByString:(NSString *)urlString BodyDic:(NSDictionary *)bodyDic WithDataBlock:(void(^)(id data))dataBlock
{
    //1.字符串转码
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //2.创建管理者对象(session)
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //3.设置允许请求的类别
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js", nil];
    //4.开始请求
    if (!bodyDic) {
        //如果bodyDic为空,执行get请求
        [manager GET:urlString parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            
            dataBlock(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
//            NSLog(@"请求失败");
            
        }];
    }
    else
    {
        //POST请求
        [manager POST:urlString parameters:bodyDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            
            dataBlock(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
//            NSLog(@"请求失败");
            
        }];
    }
}

@end
